package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.suoyao.SuoYaoHasGetRewardData;
import com.yanqu.road.entity.activity.suoyao.SuoYaoUserData;
import com.yanqu.road.entity.activity.suoyao.config.*;
import com.yanqu.road.pb.activity.SuoYaoActivityProto;
import com.yanqu.road.server.manger.ServerLanguageMgr;

import java.util.Map;

public class SuoYaoPb {

    public static SuoYaoActivityProto.SuoYaoUserDataMsg.Builder parseSuoYaoUserDataMsg(SuoYaoUserData userData) {
        SuoYaoActivityProto.SuoYaoUserDataMsg.Builder builder = SuoYaoActivityProto.SuoYaoUserDataMsg.newBuilder();
        builder.setFloorNo(userData.getFloorNo());
        builder.setScore(userData.getScore());
        builder.setSwordValue(userData.getSwordValue());
        builder.addAllRewardIdList(userData.getRewardIdList());

        SuoYaoActivityProto.SuoYaoTopRewardMsg.Builder chooseBuilder = SuoYaoActivityProto.SuoYaoTopRewardMsg.newBuilder();
        chooseBuilder.setRewardId(userData.getChooseRewardData().getRewardId());
        chooseBuilder.setIndex(userData.getChooseRewardData().getIndex());
        chooseBuilder.setIsSelected(userData.getChooseRewardData().isSelected());
        builder.setChooseTopReward(chooseBuilder);

        Map<Integer, SuoYaoHasGetRewardData> hasGetRewardData = userData.getHasGetRewardData();
        for (SuoYaoHasGetRewardData rewardData:hasGetRewardData.values()){
            for (Map.Entry<Integer,Integer> entry:rewardData.getGetRewardTime().entrySet()){
                SuoYaoActivityProto.SuoYaoTopRewardGetTimeMsg.Builder hasGetBuild = SuoYaoActivityProto.SuoYaoTopRewardGetTimeMsg.newBuilder();
                SuoYaoActivityProto.SuoYaoTopRewardMsg.Builder rewardBuild = SuoYaoActivityProto.SuoYaoTopRewardMsg.newBuilder();
                rewardBuild.setRewardId(rewardData.getRewardId());
                rewardBuild.setIndex(entry.getKey());
                hasGetBuild.setReward(rewardBuild);
                hasGetBuild.setTime(entry.getValue());

                builder.addRewardTime(hasGetBuild);
            }
        }
        builder.setRound(userData.getRound());
        builder.addAllHasGetBookRewardId(userData.getHasGetBookRewardList());

        Map<Integer, SuoYaoHasGetRewardData> hasGetSelectedData = userData.getHasGetSelectedData();
        for (SuoYaoHasGetRewardData rewardData : hasGetSelectedData.values()) {
            for (Map.Entry<Integer, Integer> entry : rewardData.getGetRewardTime().entrySet()) {
                SuoYaoActivityProto.SuoYaoTopRewardGetTimeMsg.Builder hasGetBuild = SuoYaoActivityProto.SuoYaoTopRewardGetTimeMsg.newBuilder();

                SuoYaoActivityProto.SuoYaoTopRewardMsg.Builder rewardBuild = SuoYaoActivityProto.SuoYaoTopRewardMsg.newBuilder();
                rewardBuild.setRewardId(rewardData.getRewardId());
                rewardBuild.setIndex(entry.getKey());

                hasGetBuild.setReward(rewardBuild);
                hasGetBuild.setTime(entry.getValue());

                builder.addSelectedRewardTime(hasGetBuild);
            }
        }

        return builder;
    }

    public static SuoYaoActivityProto.SuoYaoBoxItemMsg.Builder parseSuoYaoBoxItemMsg(int index, int rewardId) {
        SuoYaoActivityProto.SuoYaoBoxItemMsg.Builder builder = SuoYaoActivityProto.SuoYaoBoxItemMsg.newBuilder();
        builder.setIndex(index);
        builder.setRewardId(rewardId);
        return builder;
    }

    public static SuoYaoActivityProto.SuoYaoConfigMsg.Builder parseSuoYaoConfigMsg(SuoYaoConfig config, String language) {
        SuoYaoActivityProto.SuoYaoConfigMsg.Builder builder = SuoYaoActivityProto.SuoYaoConfigMsg.newBuilder();
        for (Map<Integer, SuoYaoRewardConfig> configMap : config.getFloorMap().values()) {
            for (SuoYaoRewardConfig rewardConfig : configMap.values()) {
                builder.addRewardConfigList(parseSuoYaoRewardConfigMsg(rewardConfig));
            }
        }
        for (SuoYaoViewBookConfig bookConfig : config.getViewBookConfigMap().values()) {
            builder.addBookConfigList(parseSuoYaoBookConfigMsg(bookConfig, language));
        }
        for (SuoYaoTopRewardConfig topRewardConfig:config.getTopRewardConfigMap().values()){
            builder.addChooseConfig(parseSuoYaoTopConfigMsg(topRewardConfig));
        }

        return builder;
    }

    public static SuoYaoActivityProto.SuoYaoRewardConfigMsg.Builder parseSuoYaoRewardConfigMsg(SuoYaoRewardConfig config) {
        SuoYaoActivityProto.SuoYaoRewardConfigMsg.Builder builder = SuoYaoActivityProto.SuoYaoRewardConfigMsg.newBuilder();
        builder.setRewardId(config.getRewardId());
        builder.setReward(config.getReward());
        builder.setFloorNo(config.getFloorNo());
        builder.setScore(config.getScore());
        builder.setIsBig(config.isBigReward());
        builder.setSort(config.getSort());
        builder.setWeight(config.getWeight());
        return builder;
    }

    public static SuoYaoActivityProto.SuoYaoBookConfigMsg.Builder parseSuoYaoBookConfigMsg(SuoYaoViewBookConfig config, String language) {
        SuoYaoActivityProto.SuoYaoBookConfigMsg.Builder builder = SuoYaoActivityProto.SuoYaoBookConfigMsg.newBuilder();
        builder.setBookId(config.getBookId());
        builder.setName(ServerLanguageMgr.getContent(config.getName(), language));
        builder.setDesc(ServerLanguageMgr.getContent(config.getDesc(), language));
        builder.setViewReward(config.getViewReward());
        builder.setLayer(config.getLayer());
        return builder;
    }

    public static SuoYaoActivityProto.SuoYaoTopChooseRewardConfigMsg.Builder parseSuoYaoTopConfigMsg(SuoYaoTopRewardConfig config){
        SuoYaoActivityProto.SuoYaoTopChooseRewardConfigMsg.Builder builder = SuoYaoActivityProto.SuoYaoTopChooseRewardConfigMsg.newBuilder();
        builder.setRound(config.getRound());
        builder.setReward(config.getReward());
        builder.setRewardId(config.getRewardId());
        builder.setPreview(config.getPreview());
        builder.setSelectedReward(config.getSelectReward());
        builder.setSelectPreview(config.getSelectPreview());
        return builder;
    }

}
